package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.ClassesMapper;
import com.hqyj.dao.RoomMapper;
import com.hqyj.pojo.Classes;
import com.hqyj.pojo.Room;
import com.hqyj.service.RoomService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {
    @Autowired
    RoomMapper roomMapper;

    @Autowired
    ClassesMapper classesMapper;

    @Override
    public Room selectById(Long Id) {
        return roomMapper.selectByPrimaryKey(Id);
    }

    @Override
    public List<Room> selectAllRoom() {
        return roomMapper.selectAllRoom();
    }

    @Override
    public int insert(Room room) {
        return roomMapper.insert(room);
    }

    @Override
    public int deleteLogic(Long id) {
        return roomMapper.deleteLogic(id);
    }

    @Override
    public HashMap<String, Object> select(Room room) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //1 设置分页参数：页码和条数
        PageHelper.startPage(room.getPage(),room.getRow());
        //2 查询结果集合
        List<Room> list = roomMapper.selectAllRoom();
        // 3 创建分页对象
        PageInfo<Room> pageInfo = new PageInfo<Room>(list);

        System.out.println("--------------------------");
        System.out.println("总条数="+pageInfo.getTotal());
        System.out.println("总页数="+pageInfo.getPages());
        System.out.println("上一页="+pageInfo.getPrePage());
        System.out.println("下一页="+pageInfo.getNextPage());
        System.out.println("首页="+pageInfo.getNavigateFirstPage());
        System.out.println("尾页="+pageInfo.getNavigateLastPage());
        System.out.println("当前页="+room.getPage());
        System.out.println("--------------------------");

        List<Room> listResult =pageInfo.getList();
        for (Room r:listResult ) {
            System.out.println(r.toString());
        }
        map.put("total",pageInfo.getTotal());
        map.put("pages",pageInfo.getPages());
        map.put("endPage",pageInfo.getNavigateLastPage());
        map.put("curPage",room.getPage());
        map.put("data",pageInfo.getList());
        System.out.println(map);

        return map;
    }

    @Override
    public HashMap<String, Object> delete(Long id) {
        int a = roomMapper.deleteLogic(id);
        HashMap<String,Object> map = new HashMap<String, Object>();
        if (a>0){
            map.put("info","删除成功");
        }else{
            map.put("info","删除失败");
        }
        return map;
    }

    @Override
    public List<Classes> findClasses() {
        return classesMapper.selectAllClasses();
    }

    @Override
    public HashMap<String, Object> insertNew(Room room) {
        room.setIsDelete(0);
        Date date = new Date(room.getTimeStamp());
        room.setUseTime(date);
        int a = roomMapper.insert(room);
        HashMap<String,Object> map = new HashMap<String, Object>();
        if (a>0){
            map.put("info","增加成功");
        }else {
            map.put("info","增加失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(Room room) {
        room.setIsDelete(0);
        Date date = new Date(room.getTimeStamp());
        room.setUseTime(date);
        System.out.println("-----------------");
        System.out.println(room.getrId());
        System.out.println(room.getrName());
        System.out.println(room.getClId());
        System.out.println(room.getUseTime());
        System.out.println("-----------------");
        int a = roomMapper.updateByPrimaryKey(room);
        HashMap<String,Object> map = new HashMap<String, Object>();
        if (a>0){
            map.put("info","修改成功");
        }else {
            map.put("info","修改失败");
        }
        return map;
    }


}
